package com.gipson.amc.service.basicdata;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsbj.common.msg.ResponseMessage;
import com.gipson.amc.entity.PageByDataTable;
import com.gipson.amc.entity.PostGroup;
import com.gipson.amc.entity.UserPostGroup;
import com.gipson.amc.mapper.UserPostGroupMapper;
import com.gipson.amc.utils.strprocess.StrUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户和岗位关联表 服务实现类
 * </p>
 *
 * @author cjw
 * @since 2020-05-21
 */
@Service
@Transactional
public class UserPostGroupService extends ServiceImpl<UserPostGroupMapper, UserPostGroup> {
	
	@Resource
	private PostGroupService postGroupService;
	
	
	
	
	public IPage<UserPostGroup> selectPageForUserPostGroup(UserPostGroup userPostGroup, PageByDataTable pageByDataTable) {
		Page<UserPostGroup> page = new Page<UserPostGroup>().setSize(pageByDataTable.getPagesize()).setCurrent(pageByDataTable.getPageindex());
		return baseMapper.queryPageData(page, userPostGroup, pageByDataTable);
	}
	
	
	/********
	 * 保存用户岗位
	 * @param postGroupId
	 * @param userIds
	 * @return
	 */
	@Transactional
	public ResponseMessage saveUserPost(String postGroupId, String userIds) {
		if (StringUtils.isNotBlank(userIds)) {
			List<String> userIdsList = new ArrayList<>();
			String[] arr = StringUtils.split(userIds, ",");
			for (int i = 0; i < arr.length; i++) {
				QueryWrapper<UserPostGroup> queryWrapper = new QueryWrapper<UserPostGroup>()
						.eq("USER_ID", arr[i])
						.eq("POST_GROUP_ID", postGroupId);
				UserPostGroup userPostGroup = baseMapper.selectOne(queryWrapper);
				if (null == userPostGroup) {
					userIdsList.add(arr[i]);
				}
			}
			if (CollectionUtils.isNotEmpty(userIdsList)) {
				for (int i = 0; i < userIdsList.size(); i++) {
					UserPostGroup userPostGroup = new UserPostGroup();
					userPostGroup.setPostGroupId(postGroupId);
					userPostGroup.setUserId(userIdsList.get(i));
					save(userPostGroup);
				}
			}
		}
		return ResponseMessage.ok("添加用户成功！");
	}
	
	@Transactional
	public ResponseMessage deleteUserPostGroup(String userId, String postGroupId) {
		Map<String, Object> map = new HashMap<>();
		map.put("USER_ID", userId);
		map.put("POST_GROUP_ID", postGroupId);
		baseMapper.deleteByMap(map);
		return ResponseMessage.ok("删除成功！");
	}
	
	/**
	 * 根据userId获取postGroup列表。
	 *
	 * @param
	 * @return
	 * @author cjw 2020-05-26
	 */
	public List<PostGroup> getPostGroupByUserId(String userId) {
		//根据userId获取list_userPostGroup
		Wrapper<UserPostGroup> wrapper_userPostGroup = Wrappers.<UserPostGroup>lambdaQuery()
				.eq(UserPostGroup::getUserId, userId);
		List<UserPostGroup> list_userPostGroup = baseMapper.selectList(wrapper_userPostGroup);
		//获取list_postGroup
		List<PostGroup> list_postGroup = new ArrayList<>();
		for (UserPostGroup userPostGroup : list_userPostGroup) {
			PostGroup postGroup = postGroupService.getById(userPostGroup.getPostGroupId());
			if (postGroup != null) {
				list_postGroup.add(postGroup);
			}
		}
		return list_postGroup;
	}
	
	
}
